var pageCount = 0;	//分页
//账单数据显示
function vendor_show(){
	$.ajax({
		url: "datalen?tableName=vendor",
		type: "POST",
		async: false,
		dataType: "json",
		success: function(date){
		pageCount = date;
		}
	}),
	//分页总数量
	$('.pagination').pagination(pageCount,
		{
		callback: vendor_PageCallback,		//PageCallback() 为翻页调用次数
		prev_text:"< 上一页",
		next_text:"下一页  >",
		items_per_page: 5,
		num_edge_entries: 2,		//两侧收尾分页条目数
		num_display_entries: 10,	//连续分页主题部分分页条目数
		current_page: 0,			//当前页索引
	});
}

//将数据读取出来
function vendor_PageCallback(page_index,jq){
	$.ajax({
		url: "do.vendor?method=0",
		type: "POST",
		async: false,
		dataType: "json",
		data: {'index':page_index + 1},
		success: function(data){
			var html = '<tr><th>供货商编号</th><th>供货商</th><th>联系人</th><th>联系电话</th><th>地址</th><th>传真号码</th><th>操作列</th></tr>';
			var list = data.vendors;
			for(var i=0;i<list.length;i++){
				html += '<tr><td>'+list[i].vendor_id+'</td><td>'
				+list[i].vendor +'</td><td>'
				+list[i].contact_person+'</td><td>'
				+list[i].vendor_phone+'</td><td>'
				+list[i].vendor_address+'</td><td>'
				+list[i].vendor_fax+'</td><td>'
				+"<input type='button' value='修改' class='update' onclick='vendor_update_show("+list[i].vendor_id+")'></td></tr>"
			}
			$('.data_table').html(html);
			$('#button').html("<input type='button' id='addshow_button' value='添加' onclick='vendor_add_show()'>");
			$('.message_main_head').text('供货商管理');
			$('.message_point').text('首页  > 供货商管理');
			pageCount = data.count;
		}
	});
}

//修改按钮事件，显示页面
function vendor_update_show(id){
	var incomeId = id;
	$.ajax({
		type:"POST",
		url:"do.vendor",
		data:"method=1&id="+id,
		dataType:"json",
		success:function(data){
			var vendor = data.update_vendor;
			var html = "";
			//设置表头
			html = "<tr><th>供货商编号</th><th>供货商</th><th>联系人</th><th>联系电话</th><th>地址</th><th>传真号码</th></tr>"
			html += "<tr>";
			html += "<td><input type='text' value='"+vendor.vendor_id+"' disabled></td>";
			html += "<td><input type='text' id='vendor' value='"+vendor.vendor+"'></td>";
			html += "<td><input type='text' id='contact_person' value='"+vendor.contact_person+"'></td>";
			html += "<td><input type='text' id='vendor_phone' value='"+vendor.vendor_phone+"'></td>";
			html += "<td><input type='text' id='vendor_address' value='"+vendor.vendor_address+"'></td>";
			html += "<td><input type='text' id='vendor_fax' value='"+vendor.vendor_fax+"'></td>";
			html += "</tr>";
			
			$('.message_main_head').text('供货商管理  > 修改');
			$('.data_table').html(html);
			//显示表格操作按钮，添加和重置
			$('#button').html("<input type='button' value='修改' onclick='vendor_update("+vendor.vendor_id+")'/><input type='button' value='重置' onclick='vendor_update_reset("+vendor.vendor_id+")'/>");
			//让分页为空
			$('.pagination').html('');
		}
	});
}

//更新数据提交事件
function vendor_update(id){
	var oldId = id;
	var vendor = $("#vendor").val().trim();
	var contact_person = $("#contact_person").val().trim();
	var vendor_phone = $("#vendor_phone").val().trim();
	var vendor_address = $("#vendor_address").val().trim();
	var vendor_fax = $("#vendor_fax").val().trim();
	
	var R = false;	//供货商格式
	var N = false;	//供货商联系人格式
	var E = false;	//供货商联系电话格式
	var S = false;	//供货商格地址式
	var X = false;	//供货商传真格式
	
	if(vendor !== "" && vendor.length<=20 ){
		if(vendor.match(/^([\u4e00-\u9fa5]*[\w]*)([\u4e00-\u9fa5]*[\w]*)+([\u4e00-\u9fa5]*[\w]*)$/)){
		R = true;
		}
	}
	if(contact_person !== "" && contact_person.length<=20 ){
		if(contact_person.match(/^([\u4e00-\u9fa5]*[\w]*)([\u4e00-\u9fa5]*[\w]*)+([\u4e00-\u9fa5]*[\w]*)$/)){
		N = true;
		}
	}
	if(vendor_phone.match("^[1-9][0-9]{10}$")){
		E = true;
	}
	if(vendor_address !== "" && vendor_address.length<=200 ){
		if(vendor_address.match(/^([\u4e00-\u9fa5]*[\w]*)([\u4e00-\u9fa5]*[\w]*)+([\u4e00-\u9fa5]*[\w]*)$/)){
		S = true;
		}
	}
	if(vendor_fax.match("^[1-9][0-9]{6,14}$")){
		X = true;
	}
	
	//数据传送到后台
	if(R && N && E && S && X){
		$.ajax({
			type:"POST",
			url:"do.vendor?method=3",
			data:"id="+oldId+"&contact_person="+contact_person+"&vendor_phone="+vendor_phone+"&vendor_address="+vendor_address+"&vendor_fax="+vendor_fax+"&vendor="+vendor,
			success:function(){
				vendor_show();
			}
		});
		alert("修改供货商成功！");
	}else{
		alert("填写信息有误，请检查！");
	}
	
}

//更新数据重置事件
function vendor_update_reset(id){
	
	vendor_update_show(id);
}

//添加供货商按钮，显示页面
function vendor_add_show(){
	$.ajax({
		url: "do.vendor",
		type: "POST",
		async: false,
		data:"method=2",
		dataType: "json",
		success: function(data){
			var html = "";
			var vendorId = data.maxId+1;
			//设置表头
			html = "<tr><th>供货商编号</th><th>供货商</th><th>联系人</th><th>联系电话</th><th>地址</th><th>传真号码</th></tr>"
			
			html += "<tr>";
			html += "<td><input type='text' id='vendor_id' value='"+vendorId+"' disabled></td>";
			html += "<td><input type='text' id='vendor'></td>";
			html += "<td><input type='text' id='contact_person'></td>";
			html += "<td><input type='text' id='vendor_phone'></td>";
			html += "<td><input type='text' id='vendor_address'></td>";
			html += "<td><input type='text' id='vendor_fax'></td>";
			html += "</tr>";
			
			$('.message_main_head').text('供货商管理  > 添加');
			$('.message_point').text('首页  > 供货商管理');
			$('.data_table').html(html);
			//显示表格操作按钮，添加和重置
			$('#button').html("<input type='button' value='添加' onclick='vendor_finish()'/><input type='button' value='重置' onclick='vendor_add_reset()'/>");
			//让分页为空
			$('.pagination').html('');
		}
	});
};

//提交按钮事件
function vendor_finish(){
	
	var vendor_id = $("#vendor_id").val().trim();
	var vendor = $("#vendor").val().trim();
	var contact_person = $("#contact_person").val().trim();
	var vendor_phone = $("#vendor_phone").val().trim();
	var vendor_address = $("#vendor_address").val().trim();
	var vendor_fax = $("#vendor_fax").val().trim();
	
	var R = false;	//供货商格式
	var N = false;	//供货商联系人格式
	var E = false;	//供货商联系电话格式
	var S = false;	//供货商格地址式
	var X = false;	//供货商传真格式
	
	if(vendor !== "" && vendor.length<=20 ){
		if(vendor.match(/^([\u4e00-\u9fa5]*[\w]*)([\u4e00-\u9fa5]*[\w]*)+([\u4e00-\u9fa5]*[\w]*)$/)){
		R = true;
		}
	}
	if(contact_person !== "" && contact_person.length<=20 ){
		if(contact_person.match(/^([\u4e00-\u9fa5]*[\w]*)([\u4e00-\u9fa5]*[\w]*)+([\u4e00-\u9fa5]*[\w]*)$/)){
		N = true;
		}
	}
	if(vendor_phone.match("^[1-9][0-9]{10}$")){
		E = true;
	}
	if(vendor_address !== "" && vendor_address.length<=200 ){
		if(vendor_address.match(/^([\u4e00-\u9fa5]*[\w]*)([\u4e00-\u9fa5]*[\w]*)+([\u4e00-\u9fa5]*[\w]*)$/)){
		S = true;
		}
	}
	if(vendor_fax.match("^[1-9][0-9]{6,14}$")){
		X = true;
	}
	
	if(R && N && E && S && X){
		$.ajax({
			type:"POST",
			url:"do.vendor?method=4",
			data:"id="+vendor_id+"&vendor="+vendor+"&contact_person="+contact_person+"&vendor_phone="+vendor_phone+"&vendor_address="+vendor_address+"&vendor_fax="+vendor_fax,
			success:function(){
				vendor_show();
			}
		});
		alert("添加供货商成功！");
	}else{
		alert("填写信息有误，请检查！");
	}
};

//添加重置按钮事件
function vendor_add_reset(){
	//重新回到添加账单状态
	vendor_add_show();
};